package com.umgsai.thread.thread23;

public class VolatileTest extends Thread {

	volatile public static int count;
	//一定要加static关键字，这样synchronized与static锁的内容就是VolatileTest类了，也就达到同步效果了。
	synchronized private static void addCount(){
		for (int i = 0; i < 100; i++) {
			count++;
		}
		System.out.println("count=" + count);
	}
	
	@Override
	public void run() {
		addCount();
	}
	
	public static void main(String[] args) {
		VolatileTest[] volatileTests = new VolatileTest[100];
		for (int i = 0; i < 100; i++) {
			volatileTests[i] = new VolatileTest();
		}
		for (int i = 0; i < 100; i++) {
			volatileTests[i].start();
		}
	}
}
